package inits

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"student/user-srv/basic/config"
	"student/user-srv/modle"
)

var err error

func MysqlInit() {
	//单例模式
	config.Once.Do(func() {
		a := config.Apps.Mysql
		dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", a.User, a.Password, a.Host, a.Port, a.Database)
		config.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
		if err != nil {
			panic(err)
		}
		fmt.Println("mysql连接成功")
		config.DB.AutoMigrate(&modle.School{}, &modle.User{}, &modle.SuperUser{})
		//连接池
		sqlDB, _ := config.DB.DB()
		sqlDB.SetMaxIdleConns(10)
		sqlDB.SetMaxOpenConns(100)
		sqlDB.SetConnMaxLifetime(0)
	})
}
